package controller;

import gui.OptionPane;

import java.rmi.RemoteException;
import java.util.Arrays;

import model.PlayerInterface;
import model.SettingsInterface;
import toolkit.Debug;

public class Options extends Controller
{
	public Options()
	{
		setView(new view.Options(this));
	}
	
	public void saveAccountSettings(char[] password, char[] confirmPassword)
	{
		if (password.length == 0)
		{
			OptionPane.showError("Empty password", "Please fill in a password.");
		}
		else if (!Arrays.equals(password, confirmPassword))
		{
			OptionPane.showError("Different passwords", "The password confirm does not match the password.");
		}
		else
		{
			try
			{
				user.setPassword(new String(password));
				
				userRemote.storeUser(user);
				
				cancel();
			}
			catch (RemoteException e)
			{
				Debug.log(e);
				
				OptionPane.showError("Error", "something went wrong when trying to save your new password.");
			}
		}
	}
	
	public void saveSettings(boolean animations, boolean autoMuck, boolean sound)
	{
		if (user instanceof PlayerInterface)
		{
			PlayerInterface player = (PlayerInterface) user;
			
			SettingsInterface settings = player.getSettings();
			
			settings.useAnimations(animations);
			settings.useAutoMuck(autoMuck);
			settings.useSound(sound);
			
			try
			{
				userRemote.setSettings(settings);
				
				cancel();
			}
			catch (RemoteException e)
			{
				Debug.log(e);
				
				OptionPane.showError("Error", "something went wrong when trying to save your settings.");
			}
		}
	}
	
	public void cancel()
	{
		forward(new controller.Lobby());
	}
}
